<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>axios 调用方法发送请求</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.27.2/axios.min.js"></script>
  </head>
  <body>
    <button>get</button>
    <button>post</button>

    <script>
      //获取按钮
      let btns = document.querySelectorAll("button");      //获取所有的书籍
      // get  delete  使用形式一样
      btns[0].onclick = async function () {
        try {
          // 使用语法axios.get(url [, config]);    a=100&b=200
          let res = await axios.get("http://127.0.0.1:3000/book");

          console.log(res);
        } catch (e) {
          console.log("请求失败~~");
        }
      };
      //新增一个书籍
      btns[1].onclick = async function () {
        try {
          //put  patch  post 使用形式一样
          // 使用语法axios.get(url [, data] [,config]);    
          let res = await axios.post("http://127.0.0.1:3000/book", {title: '狼图腾', price: 19.9});
          console.log(res);
        } catch (e) {
          console.log("请求失败~~");
        }
      };
    </script>
  </body>
</html>
